package cn.yjr.vector;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.yjr.vector.tools.MD5;
import cn.yjr.vector.tools.SharedPreferencesHelper;
import cn.yjr.vector.tools.YJRHttpClient;
import cn.yjr.vectori.adapter.ExpListAdapter;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

public class ExperienceActivity extends SherlockActivity implements OnClickListener{

	LayoutInflater inflater;
	ActionBar actionBar;
	View title_bar;
	Context context;
	String response;
	JSONObject jsonInfo;
	ListView exp_list;
	JSONArray exps;
	ExpListAdapter myAdapter;
	public static boolean[] checked;
	public static int state = 1;
	TextView add_exp_tv, edit_tv, delete_tv;
	SharedPreferencesHelper spHelper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_experience);
		inflater = this.getLayoutInflater();
		actionBar = getSupportActionBar();
		context = this;
		this.spHelper = new SharedPreferencesHelper(this.context);
		initTitleBar();
		initComponent();
		
		initEventListener();
		getList();
	
	}
	 
	
	private void initTitleBar(){
		ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.WRAP_CONTENT,Gravity.CENTER);
		title_bar = inflater.inflate(R.layout.experience_action_bar, null);
		
		actionBar.setCustomView(title_bar,lp);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);
	}
	private void initComponent(){
		add_exp_tv = (TextView) findViewById(R.id.add_exp);
		edit_tv = (TextView) title_bar.findViewById(R.id.edit);
		delete_tv = (TextView) title_bar.findViewById(R.id.delete);
		exp_list = (ListView) findViewById(R.id.exp_list);
	}
	
	private void initEventListener(){
		add_exp_tv.setOnClickListener(this);
		edit_tv.setOnClickListener(this);
		delete_tv.setOnClickListener(this);

		
	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.add_exp:
			Intent intent = new Intent(ExperienceActivity.this, AddExperienceActivity.class);
			startActivityForResult(intent,1);
			break;
		case R.id.edit:
			state = 2;
			edit_tv.setVisibility(View.INVISIBLE);
			delete_tv.setVisibility(View.VISIBLE);
			myAdapter.notifyDataSetChanged();
			break;
		case R.id.delete:
			state = 1;
			edit_tv.setVisibility(View.VISIBLE);
			delete_tv.setVisibility(View.INVISIBLE);
			//TODO 删除了以后应该重新请求下从医经历，然后刷新显示
			for(int i = 0; i < checked.length; i++){
				if(checked[i]){
					try {
						long id = exps.getJSONObject(i).getLong("id");
						delete(id+"");
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
			getList();
			break;
		default:
				break;
		}
		
	}
	
	private void getList(){

		YJRHttpClient.post(context,"/doctor/listExperiencesOfDoctor",null, new JsonHttpResponseHandler(){
			 @Override  
	            public void onSuccess(JSONObject data) {  
	             	jsonInfo = data;
	             	try {
						if(jsonInfo.getInt("result") == 1){
							YJRHttpClient.setCookie(context); 
							exps = data.getJSONArray("response");
							checked = new boolean[exps.length()];
							for(int i = 0; i < exps.length(); i++){
								checked[i] = false;
							}
							
							myAdapter = new ExpListAdapter(exps, context);
							exp_list.setAdapter(myAdapter);
							myAdapter.notifyDataSetChanged();
							
						}else{
							Toast.makeText(context, jsonInfo.getString("info"), Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }  
		});
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
	      switch (requestCode) {
	      case 1:
	    	  //TODO
	    	  Toast.makeText(context, "页面重新加载", Toast.LENGTH_SHORT).show();
	    	  getList();
	  		  myAdapter.notifyDataSetChanged();
	    	  break;
	      default:  
	          break;  

	      }  
	      super.onActivityResult(requestCode, resultCode, data);  
	  }  

	private void delete(String id){
		RequestParams params = new RequestParams(); 
		params.add("deId", id);
		YJRHttpClient.post(context,"/doctor/removeExperience",params, new JsonHttpResponseHandler(){
			 @Override  
	            public void onSuccess(JSONObject data) {  
	             	jsonInfo = data;
	             	try {
						if(jsonInfo.getInt("result") == 1){
							YJRHttpClient.setCookie(context); 
							Toast.makeText(context, "删除成功", Toast.LENGTH_SHORT).show();
						}else{
							Toast.makeText(context, jsonInfo.getString("info"), Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }  
		});
	}
}